<!DOCTYPE html>

<html lang="en" data-content_root="../../">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />

    <title>Scratch hat blocks → Pytch decorators &#8212; Pytch  documentation</title>
    <link rel="stylesheet" type="text/css" href="../../_static/pygments.css?v=03e43079" />
    <link rel="stylesheet" type="text/css" href="../../_static/classic.css?v=36340f97" />
    <link rel="stylesheet" type="text/css" href="../../_static/css/pytch-classic.css?v=0321735e" />
    
    <script src="../../_static/documentation_options.js?v=7f41d439"></script>
    <script src="../../_static/doctools.js?v=9bcbadda"></script>
    <script src="../../_static/sphinx_highlight.js?v=dc90522c"></script>
    
    <link rel="icon" href="../../_static/favicon.ico"/>
    <link rel="author" title="About these documents" href="../../about.html" />
    <link rel="index" title="Index" href="../../genindex.html" />
    <link rel="search" title="Search" href="../../search.html" />
    <link rel="next" title="Defining costumes" href="costume-specs.html" />
    <link rel="prev" title="Functions in the pytch module" href="pytch-module-functions.html" /> 
  </head><body>
<div class="NavBar">
  <a href="../../../app/"><h1>Pytch</h1></a>
  <ul>
    <a href="https://pytch.scss.tcd.ie/"><li>About Pytch</li></a>
    <a href="../../index.html"><li>Help</li></a>
    <a href="../../../app/tutorials/"><li>Tutorials</li></a>
    <a href="../../../app/my-projects/"><li>My projects</li></a>
  </ul>
</div>
<div class="warning-work-in-progress">
  <p>These help pages are incomplete — we are working on it!</p>
</div>
  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <section id="scratch-hat-blocks-pytch-decorators">
<h1>Scratch hat blocks → Pytch decorators<a class="headerlink" href="#scratch-hat-blocks-pytch-decorators" title="Link to this heading">¶</a></h1>
<p>Done via Python <em>decorators</em>. E.g.,</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="nd">@pytch</span><span class="o">.</span><span class="n">when_I_receive</span><span class="p">(</span><span class="s2">&quot;Play_One_Point&quot;</span><span class="p">)</span>
<span class="k">def</span><span class="w"> </span><span class="nf">prepare_to_play</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
    <span class="c1"># ... do stuff ...</span>
</pre></div>
</div>
<p>The available decorators are:</p>
<dl class="py attribute">
<dt class="sig sig-object py" id="pytch.when_green_flag_clicked">
<span class="sig-prename descclassname"><span class="pre">pytch.</span></span><span class="sig-name descname"><span class="pre">when_green_flag_clicked</span></span><a class="headerlink" href="#pytch.when_green_flag_clicked" title="Link to this definition">¶</a></dt>
<dd><p>Ask Pytch to run the decorated method whenever the green flag is
clicked by the user.</p>
</dd></dl>

<dl class="py function">
<dt class="sig sig-object py" id="pytch.when_key_pressed">
<span class="sig-prename descclassname"><span class="pre">pytch.</span></span><span class="sig-name descname"><span class="pre">when_key_pressed</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">key_name</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#pytch.when_key_pressed" title="Link to this definition">¶</a></dt>
<dd><p>Ask Pytch to run the decorated method whenever the user presses the
given key.</p>
</dd></dl>

<dl class="py attribute">
<dt class="sig sig-object py" id="pytch.when_this_sprite_clicked">
<span class="sig-prename descclassname"><span class="pre">pytch.</span></span><span class="sig-name descname"><span class="pre">when_this_sprite_clicked</span></span><a class="headerlink" href="#pytch.when_this_sprite_clicked" title="Link to this definition">¶</a></dt>
<dd><p>Ask Pytch to run the decorated method whenever the user clicks (or
taps) on the sprite.  Should be used only for Sprites (not your
Stage).</p>
</dd></dl>

<dl class="py attribute">
<dt class="sig sig-object py" id="pytch.when_stage_clicked">
<span class="sig-prename descclassname"><span class="pre">pytch.</span></span><span class="sig-name descname"><span class="pre">when_stage_clicked</span></span><a class="headerlink" href="#pytch.when_stage_clicked" title="Link to this definition">¶</a></dt>
<dd><p>Ask Pytch to run the decorated method whenever the user clicks (or
taps) on the stage.  Should be used only for your Stage (not
Sprites).</p>
</dd></dl>

<dl class="py function">
<dt class="sig sig-object py" id="pytch.when_I_receive">
<span class="sig-prename descclassname"><span class="pre">pytch.</span></span><span class="sig-name descname"><span class="pre">when_I_receive</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">message_string</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#pytch.when_I_receive" title="Link to this definition">¶</a></dt>
<dd><p>Ask Pytch to run the decorated method whenever somebody broadcasts
the given <code class="docutils literal notranslate"><span class="pre">message_string</span></code>.</p>
</dd></dl>

<dl class="py attribute">
<dt class="sig sig-object py" id="pytch.when_I_start_as_a_clone">
<span class="sig-prename descclassname"><span class="pre">pytch.</span></span><span class="sig-name descname"><span class="pre">when_I_start_as_a_clone</span></span><a class="headerlink" href="#pytch.when_I_start_as_a_clone" title="Link to this definition">¶</a></dt>
<dd><p>Ask Pytch to run the decorated method whenever a clone of the
Sprite is created.  Within the method, <code class="docutils literal notranslate"><span class="pre">self</span></code> refers to the
newly-created clone.</p>
</dd></dl>

</section>


            <div class="clearer"></div>
          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="Main">
        <div class="sphinxsidebarwrapper"><ul class="current">
<li class="toctree-l1"><a class="reference internal" href="../../webapp/user/index.html">Using the Pytch web app</a></li>
<li class="toctree-l1 current"><a class="reference internal" href="index.html">Writing Pytch programs</a><ul class="current">
<li class="toctree-l2"><a class="reference internal" href="sprite.html">Sprites</a></li>
<li class="toctree-l2"><a class="reference internal" href="stage.html">The stage</a></li>
<li class="toctree-l2"><a class="reference internal" href="non-hat-blocks.html">Scratch blocks → Pytch functions and methods</a></li>
<li class="toctree-l2"><a class="reference internal" href="pytch-module-functions.html">Functions in the pytch module</a></li>
<li class="toctree-l2 current"><a class="current reference internal" href="#">Scratch hat blocks → Pytch decorators</a><ul>
<li class="toctree-l3"><a class="reference internal" href="#pytch.when_green_flag_clicked"><code class="docutils literal notranslate"><span class="pre">pytch.when_green_flag_clicked</span></code></a></li>
<li class="toctree-l3"><a class="reference internal" href="#pytch.when_key_pressed"><code class="docutils literal notranslate"><span class="pre">pytch.when_key_pressed()</span></code></a></li>
<li class="toctree-l3"><a class="reference internal" href="#pytch.when_this_sprite_clicked"><code class="docutils literal notranslate"><span class="pre">pytch.when_this_sprite_clicked</span></code></a></li>
<li class="toctree-l3"><a class="reference internal" href="#pytch.when_stage_clicked"><code class="docutils literal notranslate"><span class="pre">pytch.when_stage_clicked</span></code></a></li>
<li class="toctree-l3"><a class="reference internal" href="#pytch.when_I_receive"><code class="docutils literal notranslate"><span class="pre">pytch.when_I_receive()</span></code></a></li>
<li class="toctree-l3"><a class="reference internal" href="#pytch.when_I_start_as_a_clone"><code class="docutils literal notranslate"><span class="pre">pytch.when_I_start_as_a_clone</span></code></a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="costume-specs.html">Defining costumes</a></li>
<li class="toctree-l2"><a class="reference internal" href="backdrop-specs.html">Defining backdrops</a></li>
<li class="toctree-l2"><a class="reference internal" href="sound-specs.html">Defining sounds</a></li>
<li class="toctree-l2"><a class="reference internal" href="multi-threading.html">Running many scripts at once</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../about.html">About Pytch</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../contact.html">Contact</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../developer.html">Developer documentation</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../legal/index.html">Legal information</a></li>
</ul>
<div class="docs-home-link"><hr>
  <ul>
    <li>
      <a href="../../index.html">Pytch help home</a>
    <li>
  </ul>
</div>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
  </body>
</html>